﻿using Dapper;
using MQ.Core.Common;
using MQ.Core.Db;
using MQ.Core.Entity.System;
using MQ.Core.IRepository.System;
using MQ.Core.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace MQ.Core.Repository.System
{
    public class AreaRepository : IAreaRepository
    {
        private readonly ISqlDb db;

        public AreaRepository(ISqlDb db)
        {
            this.db = db;
        }
        public void Add(AreaEntity entity)
        {
            string sql = $"insert into sys_area(name,code,parentcode,[level]) values(@name,@code,@parentCode,@level)";
            db.Execute(sql, entity);
        }

        public void Delete(string ids)
        {
            throw new NotImplementedException();
        }

        public void Delete(long id)
        {
            throw new NotImplementedException();
        }

        public void Delete(List<long> ids)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<AreaEntity> GetList(string query)
        {
            string sql = $"select * from sys_area where 1=1";
            var json = query.ToJObject();
            if (!json["code"].IsEmpty())
            {
                sql += $" and code='{json["code"].ToString()}'";
            }
            if (!json["name"].IsEmpty())
            {
                sql += $" and name='{json["name"].ToString()}'";
            }
            if (!json["parentCode"].IsEmpty())
            {
                sql += $" and parentCode='{json["parentCode"].ToString()}'";
            }
            if (!json["keyword"].IsEmpty())
            {
                sql += $" and keyword like'%{json["keyword"].ToString()}%'";
            }
            return db.GetList<AreaEntity>(sql);
        }

        public IEnumerable<AreaEntity> GetPageList(Pagination pagination, string query)
        {
            throw new NotImplementedException();
        }

        public void Update(AreaEntity entity)
        {
            throw new NotImplementedException();
        }
    }
}
